package com.fanxj.controller;

import com.fanxj.Utils.ResponseData;
import com.fanxj.domain.User;
import com.fanxj.service.AdminService;
import com.fanxj.service.SuperAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/SuperAdmin")
public class SuperAdminController {
    @Autowired
    private SuperAdminService superAdminService;
    @RequestMapping(value = "/findAllUser",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData findAllUser(){
        List<User> userList=superAdminService.findAllUser();
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("userList", userList);
        return responseData;
    }

    @RequestMapping(value = "/findUserByName",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData findUserByName(String userName){
        List<User> userList=superAdminService.findUserByName("%"+userName+"%");
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("userList", userList);
        return responseData;
    }

    @RequestMapping(value = "/UpdateRole",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData UpdateRole(User user){
        superAdminService.UpdateRole(user);

        ResponseData responseData = ResponseData.ok();
        return responseData;
    }

    //注册
    @RequestMapping(value = "/saveAdmin",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public  ResponseData saveAdmin(User user){
        String message=superAdminService.saveAdmin(user);
        System.out.println(message);
        ResponseData responseData=null;
        if(message.equals("success")){
            responseData = ResponseData.ok();
        }else{
            responseData = ResponseData.UserExist();
        }
        return responseData;
    }

    @RequestMapping(value = "/findAdminByName",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData findAdminByName(String userName){
        List<User> userList=superAdminService.findAdminByName("%"+userName+"%");
        ResponseData responseData = ResponseData.ok();
        responseData.putDataValue("userList", userList);
        return responseData;
    }

    @RequestMapping(value = "/deleteAdmin",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResponseData deleteAdmin(User user){
        superAdminService.deleteAdmin(user);
        ResponseData responseData = ResponseData.ok();
        return responseData;
    }


}
